#!/bin/sh
# file name mfcommand
# modified by Toan Nguyen, June 2012

#     Copyright © 2012-2013 Toan Nguyen <toan.nguyen@monash.edu>
#
#     This file is part of Xglom.
# 
#     This is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
# 
#     This is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
#     You should have received a copy of the GNU General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.


# Function: logon
#
logon() {

    if test -f "$MFLUX_SID_FILE" 
    then {
      logoff
    }
    fi

    MFLUX_SID=$(java -Djava.net.preferIPv4Stack=true -Dmf.host=$MFLUX_HOST -Dmf.port=$MFLUX_PORT -Dmf.transport=$MFLUX_TRANSPORT -cp ./aterm.jar arc.mf.command.Execute logon "$1" "$2" "$3")
    RETVAL=$?

    case $RETVAL in 
      0) echo "$MFLUX_SID" >> "$MFLUX_SID_FILE"
         echo "Logged on successfully!"
      ;;
      2) echo "Authentication failure"
      ;;
    esac
}

# Function: help
#
#  This executes displays command help
#
help() {

    if test -f "$MFLUX_SID_FILE"
    then {
      MFLUX_SID=$(cat "$MFLUX_SID_FILE")

      java -Djava.net.preferIPv4Stack=true -Dmf.host=$MFLUX_HOST -Dmf.port=$MFLUX_PORT -Dmf.transport=$MFLUX_TRANSPORT -Dmf.sid="$MFLUX_SID" -Dmf.result=shell -cp ./aterm.jar arc.mf.command.Execute "$@"

      RETVAL=$?

      case $RETVAL in 
        3) echo "Session has timed out - need to logon again."; 
           rm -f "$MFLUX_SID_FILE"
        ;;
      esac

    } else {

      echo "Not logged on"

      RETVAL=1      
    }
    fi

}

# Function: execute
#
#  This executes an arbitrary command.
#
execute() {
    if test -f "$MFLUX_SID_FILE"
    then {
      MFLUX_SID=$(cat "$MFLUX_SID_FILE")

      java -Djava.net.preferIPv4Stack=true -Dmf.host=$MFLUX_HOST -Dmf.port=$MFLUX_PORT -Dmf.transport=$MFLUX_TRANSPORT -Dmf.sid="$MFLUX_SID" -Dmf.result=shell -cp ./aterm.jar arc.mf.command.Execute "$@"

      RETVAL=$?

      case $RETVAL in 
        3) echo "Session has timed out - need to logon again."; 
           rm -f "$MFLUX_SID_FILE"
        ;;
      esac

    } else {

      echo "Not logged on"

      RETVAL=1      
    }
    fi

}


# Function: import
#
#  This executes an arbitrary command.
#
import() {

    if test -f "$MFLUX_SID_FILE"
    then {
      MFLUX_SID=$(cat "$MFLUX_SID_FILE")

      java -Djava.net.preferIPv4Stack=true -Dmf.host=$MFLUX_HOST -Dmf.port=$MFLUX_PORT -Dmf.transport=$MFLUX_TRANSPORT -Dmf.sid="$MFLUX_SID" -Dmf.result=shell -cp ./aterm.jar arc.mf.command.Execute import "$@"

      RETVAL=$?

      case $RETVAL in 
        3) echo "Session has timed out - need to logon again."; 
           rm -f "$MFLUX_SID_FILE"
        ;;
      esac

    } else {

      echo "Not logged on"

      RETVAL=1      
    }
    fi

}


# Function: logoff
#
logoff() {

    if test -f "$MFLUX_SID_FILE"
    then {
      MFLUX_SID=$(cat "$MFLUX_SID_FILE")

      # Remove the file now..
      rm -f "$MFLUX_SID_FILE"

      java -Djava.net.preferIPv4Stack=true -Dmf.host=$MFLUX_HOST -Dmf.port=$MFLUX_PORT -Dmf.transport=$MFLUX_TRANSPORT -Dmf.sid="$MFLUX_SID" -cp ./aterm.jar arc.mf.command.Execute logoff

      echo "Logged off successfully!"

      RETVAL=$?
    } else {

      echo "Not logged on"

      RETVAL=1      
    }
    fi

}

# Function: status
#
status() {
    if test -f "$MFLUX_SID_FILE"
    then {
      MFLUX_SID=$(cat "$MFLUX_SID_FILE")
      echo "Logged on in session $MFLUX_SID"
    } else {
      echo "Not logged on"
    }
    fi

    RETVAL=1
}

# check parameters
if [ ! -f ~/.daris_current_parameters ]; then
     echo "Error: cannot find parameters, try logon first!"
     exit 1;
fi
. ~/.daris_current_parameters

# Options:
#
case "$1" in 
  logon) 
    logon "$2" "$3" "$4" 
    ;;

  logoff)
    logoff
    ;;

  import)
    import "$2" "$3" "$4" "$5" "$6" "$7" "$8"
    ;;

  status)
    status
    ;;

  help)
    help "$@"
    ;;

  --help)
    echo $"Usage: $0 {logon|logoff|status|help|<mediaflux service>}"
    RETVAL=1
    ;;

  *) 
    execute "$@"
    ;;

esac

exit $RETVAL

